package com.hb.trans.mvp.ui.fragment


import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.basiclib.base.BaseFragment
import com.hb.trans.R
import com.hb.trans.bean.ArticleLabel
import com.hb.trans.mvp.contract.NewsContract
import com.hb.trans.mvp.presenter.NewsPresenter
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_news.*
import javax.inject.Inject

/**
 * 资讯主页
 */
class NewsFragment : BaseFragment<NewsPresenter>(), NewsContract.View {
    override fun setViewId(): Int {
        return R.layout.fragment_news
    }

    override fun doOnInvisible() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun doOnVisible() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    @Inject
    lateinit var mPresenter: NewsPresenter

//    lateinit var rootView: View

    companion object {
        fun newInstance(): NewsFragment {
            val fragment = NewsFragment()
            return fragment
        }
    }

//    override fun initView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
//        rootView = inflater.inflate(R.layout.fragment_news, container, false)
//        return rootView
//    }

    override fun onFragmentInject() {
        AndroidSupportInjection.inject(this)
    }

    override fun attachView() {
        mPresenter.attachView(this)
    }

    override fun initView() {

    }

    override fun setupData(labels: MutableList<ArticleLabel>?) {
        labels?.let {
            viewPager.adapter = NewsPageAdapter(childFragmentManager, labels)
            for (label in labels) {
                tabLayout.addTab(tabLayout.newTab().apply {
                    text = label.name
                    tag = label.id
                })
            }
            tabLayout.setupWithViewPager(viewPager)
        }


    }

    override fun initData() {
    }

    override fun onDestroy() {
        super.onDestroy()
//        ImmersionBar.with(this).destroy()
    }

    class NewsPageAdapter(
        fm: androidx.fragment.app.FragmentManager,
        private val labels: MutableList<ArticleLabel>
    ) : androidx.fragment.app.FragmentPagerAdapter(fm) {
        override fun getItem(position: Int): androidx.fragment.app.Fragment {
            return NewListFragment.newInstance(labels[position].id)
        }

        override fun getCount(): Int = labels.size

        override fun getPageTitle(position: Int): CharSequence? {
            return labels[position].name
        }

    }
}
